/*
 *  Copyright (C) 2011 Dan de la Cruz <ddcruzg@gmail.com>
 * 
 *  Viajero Fashion is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  Viajero Fashion is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.vfashion.model;

import com.vfashion.dao.ClienteDAO;
import com.vfashion.dao.PaqueteDAO;
import com.vfashion.dao.VentaDAO;
import com.vfashion.dao.factory.DAOFactory;
import com.vfashion.domain.Carrito;
import com.vfashion.domain.Cliente;
import com.vfashion.domain.Paquete;
import java.util.List;

/**
 * @author Dan de la Cruz <ddcruzg@gmail.com>
 */

public class VfashionModel {

    public VfashionModel() {
    }

    public Cliente login(String usuario, String clave) throws Exception {

        ClienteDAO daoCliente = DAOFactory.getInst().getClienteDAO();
        Cliente cliente = daoCliente.getByLogin(usuario, clave);
        if (!usuario.equals(cliente.getUsuario()) || !clave.equals(cliente.getClave())) {
            throw new Exception("usuario y/o contraseña invalido");
        }
        return cliente;
    }

    public List<Paquete> listPaquetes() throws Exception {

        PaqueteDAO daoPaquete = DAOFactory.getInst().getPaqueteDAO();
        List<Paquete> lstPaquetes = daoPaquete.getAll();
        if (lstPaquetes == null) {
            throw new Exception("no tenemos paquetes turisticos por el momento");
        }
        return lstPaquetes;
    }

    public Paquete getPaquete(int idpaquete) throws Exception {

        PaqueteDAO daoPaquete = DAOFactory.getInst().getPaqueteDAO();
        Paquete paquete = daoPaquete.getById(idpaquete);
        System.out.println("lugar : "+paquete.getLugar());
        if(paquete.getIdpaquete() != idpaquete){
            throw new Exception("no tenemos detalles del paquete turistico");
        }
        System.out.println("retornando paquete del model");
        return paquete;
    }

    public void realizarVenta(Cliente cliente,Carrito carrito) throws Exception{

        if(cliente == null || cliente.getIdcliente() <= 0 ){
             throw new Exception("session");
        }

        if(carrito == null || carrito.getCantidad() <= 0){
             throw new Exception("No tiene paquetes turisticos en su carrito");
        }

        VentaDAO daoVenta = DAOFactory.getInst().getVentaDAO();
        daoVenta.realizarVenta(cliente, carrito);
        carrito.quitarProductos();
    }
}
